<script setup>
import { onMounted, reactive, ref } from "vue";
import axios from "axios";
import { ElMessage } from "element-plus";
import { size } from "lodash";

const addUser = ref(false);
const upUser = ref(false);
const data = reactive({
  userList: [],
  user: {
    age: 1,
    sex: "男",
    status: 2,
  },
  userFrom: {},
  pageIndex: 1,
  size: 10,
  total: 0,
});
const getUserList = () => {
  axios
    .get("/user/getUserList/" + data.pageIndex + "/" + data.size)
    .then((res) => {
      data.userList = res.data.data.records;
      data.total = res.data.data.total;
    });
};
const editUser = (id) => {
  axios.get("/user/getUser/" + id).then((res) => {
    data.userFrom = res.data.data;
    upUser.value = true;
  });
};
const updateUser = () => {
  axios.put("/user/updateUser", data.userFrom).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
    }
    upUser.value = false;
    getUserList();
  });
};
const insertUser = () => {
  axios.post("/user/insertUser", data.user).then((res) => {
    if (res.data.code == 200) {
      addUser.value = false;
      ElMessage.success("添加成功");
    } else {
      ElMessage.error(res.data.message);
    }
    getUserList();
  });
};
const deleteUser = (id) => {
  axios.delete("/user/deleteUser/" + id).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("删除成功");
    }
    getUserList();
  });
};
const handleSizeChange = () => {
  getUserList();
};
const handleCurrentChange = () => {
  getUserList();
};
getUserList();
</script>
<template>
  <div>
    <el-button class="fr" type="primary" @click="addUser = true"
      >添加用户</el-button
    >
    <div style="margin: 10px">
      <el-breadcrumb :separator-icon="ArrowRight">
        <el-breadcrumb-item>后台管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-table :data="data.userList" stripe border>
      <el-table-column type="index" label="序号" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="name" label="昵称"> </el-table-column>
      <el-table-column prop="sex" label="性别"> </el-table-column>
      <el-table-column prop="age" label="年龄"> </el-table-column>
      <el-table-column prop="status" label="用户权限">
        <template #default="scope">
          <el-tag class="ml-2" type="success" v-if="scope.row.status == 2"
            >普通用户</el-tag
          >
          <el-tag class="ml-2" type="warning" v-else>管理员</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column fixed="right" label="操作" width="220">
        <template #default="scope">
          <el-tooltip
            class="box-item"
            content="编辑"
            placement="top"
            effect="light"
          >
            <el-button type="primary" @click="editUser(scope.row.id)">
              <el-icon>
                <Edit />
              </el-icon>
            </el-button>
          </el-tooltip>
          <el-tooltip
            class="box-item"
            content="删除"
            placement="top"
            effect="light"
          >
            <el-button type="danger" @click="deleteUser(scope.row.id)">
              <el-icon>
                <Delete />
              </el-icon>
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        v-model:currentPage="data.pageIndex"
        v-model:page-size="data.size"
        :page-sizes="[1, 2, 5, 10]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="data.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog v-model="addUser" title="添加用户" width="30%">
      <el-form :model="data.user" label-position="top">
        <el-form-item label="用户名">
          <el-input v-model="data.user.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="data.user.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="data.user.password" type="password" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="data.user.sex" class="ml-4">
            <el-radio label="男" size="large">男</el-radio>
            <el-radio label="女" size="large">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input-number v-model="data.user.age" :min="1" :max="100" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="data.user.status" class="ml-4">
            <el-radio-button label="2">普通用户</el-radio-button>
            <el-radio-button label="1">管理员</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addUser = false">取消</el-button>
          <el-button type="primary" @click="insertUser()">提交</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog v-model="upUser" title="修改用户" width="30%">
      <el-form :model="data.userFrom" label-position="top">
        <el-form-item label="姓名">
          <el-input v-model="data.userFrom.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="data.userFrom.password"/>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="data.userFrom.sex" class="ml-4">
            <el-radio label="男" size="large">男</el-radio>
            <el-radio label="女" size="large">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input-number v-model="data.userFrom.age" :min="1" :max="100" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="data.userFrom.status" class="ml-4">
            <el-radio-button label="2">普通用户</el-radio-button>
            <el-radio-button label="1">管理员</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addUser = false">取消</el-button>
          <el-button type="primary" @click="updateUser()">提交</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<style scoped>
.block {
  margin-top: 20px;
}

.fr {
  float: right;
}
</style>
